package iredis

import (
	"gitee.com/tietang/terrace-go/v2/base"
	"github.com/go-redis/redis/v8"
	log "github.com/sirupsen/logrus"
	"github.com/tietang/props/v3/kvs"
)

var redisClusterClient *redis.ClusterClient

//集群版本
func Rdbx() *redis.ClusterClient {
	return GetRedisClusterClient()
}

func GetRedisClusterClient() *redis.ClusterClient {
	base.Check(redisClusterClient)
	return redisClusterClient
}

func initRedisClusterClient(conf kvs.ConfigSource) *redis.ClusterClient {
	opts := &redis.ClusterOptions{
		//Addr:     conf.GetDefault("redisCluster.addr", "127.0.0.1:6379"),
		//Password: conf.GetDefault("redisCluster.password", ""),
		//DB:       conf.GetIntDefault("redisCluster.database", 0),
		//PoolSize: conf.GetIntDefault("redisCluster.poolSize", 3),
	}
	err := kvs.Unmarshal(conf, opts, "redisCluster")
	if err != nil {
		panic(err)
	}
	log.Infof("connecting redisCluster: %+v", opts)
	client := redis.NewClusterClient(opts)
	log.Infof("connected redisCluster: %+v", client.Options())
	return client
}

type redisClusterStarter struct {
	base.BaseStarter
}

func (s *redisClusterStarter) Init(ctx base.StarterContext) {
	initRedisClusterClient(ctx.Props())
	ctx["redisCluster"] = redisClusterClient
}
func (s *redisClusterStarter) Setup(ctx base.StarterContext) {

}
